import {Injectable} from "@angular/core";
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from "@angular/common/http";
import {Observable} from "rxjs/Observable";
import {TokenUtil} from "./token-util";
import {AppConfig} from "../app.config";

@Injectable()
export class TokenInterceptor implements HttpInterceptor {
  constructor(private tokenUtil: TokenUtil) {
  }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    let authReq = req;
    if(AppConfig.authType && 'jwt'== AppConfig.authType.toLowerCase()){
      let token = this.tokenUtil.getToken();
      authReq = req.clone({headers: req.headers.set('Authorization', "Bearer " + token)});
    }
    return next.handle(authReq);
  }
}

